
package ar.com.photo_admin.service

import ar.com.photo_admin.domain.Event
import ar.com.photo_admin.domain.Message
import ar.com.photo_admin.domain.PurchaseOrder;
import ar.com.photo_admin.domain.User

class CustomMailService extends BaseService {
	
	def notifyNewMessage(Message message, url) {
		
		log.info "Enviando mail de nuevo mensaje"
		
		def subjectM = getMessage("messages.received.success")
		def viewUrl = "/customMail/newMessage"
		
		try{
			sendMail {
				to message.userMessage.to.email
				body(view: viewUrl,
					 model: [message:message, url:url])
				subject subjectM
			}
		}
		catch(Exception e) {
			log.error "Se ha producido un error intentando mandar el mail a " + message.userMessage.to.email
			log.error e
		}								
	}
	
	def notifyNewOrder(PurchaseOrder purchaseOrder,photographer){
				
		log.info "Enviando mail de confirmacion de orden"
		
		def exportedFilePath = getOrdersRootPath()+purchaseOrder.id+".pdf"
		log.info "Se adjunta el archivo exportado: ${exportedFilePath}"
		def orderFile = new File(getOrdersRootPath()+purchaseOrder.id+".pdf")
		
		def subjectM = getMessage("notification.order.confirmed." + (photographer ? "photographer" : "client"))
		def viewUrl = "/customMail/orderConfirmed"+ (photographer ? "Photographer" : "Client")
		
		try{
			sendMail {
				multipart true
				to purchaseOrder.photographer.email										
				body( 	view: viewUrl,							
						model:[purchaseOrderInstance:purchaseOrder,
								mailStyle : createStyle()])
				subject subjectM
				attach "detalle.pdf", "application/pdf", orderFile
			}
		}
		catch(Exception e) {
			log.error "Se ha producido un error intentando mandar el mail"
			log.error e
		}					
	}
	
	def notifyOrderStatusChange(PurchaseOrder purchaseOrder, photographer){
					
		log.info "Enviando mail de cambio de estado "
		
		def subjectM = getMessage("notification.order.status.changed." +( photographer ? "photographer" : "client"))
		def viewUrl = "/customMail/orderStatusChange"+ (photographer ? "Photographer" : "Client")
																	
		try{
			sendMail {
				multipart true
				to purchaseOrder.photographer.email
				body( 	view: viewUrl,
						model:[purchaseOrderInstance:purchaseOrder,
								mailStyle : createStyle()])
				subject subjectM				
			}
		}
		catch(Exception e) {
			log.error "Se ha producido un error intentando mandar el mail"
			log.error e
		}
	}
	
	def sendShareEventInfo(def token, def tokenUrl, User user, Event event) {
		
		def subjectM = getMessage("share.event.info2")
		def viewUrl = "/customMail/shareInfo"
		
		try{
			sendMail {
				to user.email
				body(view: viewUrl,
					 model: [token : token, tokenUrl : tokenUrl, event : event.name, mailStyle : createStyle()])
				subject subjectM
			}
		}
		catch(Exception e) {
			log.error "Se ha producido un error intentando mandar el mail a " + user.email
			log.error e
			return false
		}
		
		return true
	}
	
	def createStyle() {
		def styleObject = [:]
		styleObject.key = "float:left; width:150px; padding-left:10px;"
		styleObject.value = "float:left; font-weight:bold;"
		return styleObject
	}
}

